import os
import unittest
import ddt
from common.browser import open_browser
from page.page_login_admin import Admin
from page.page_login_admin_after import After


# 准备数据
admin_account = [{'username': 'admin', 'password': 'admin1234'}]
admin_fail_account = [{'username': 'admin1', 'password': 'admin1234'},
                      {'username': 'admin', 'password': 'admin123'}]


@ddt.ddt
class TestAdminLogin(unittest.TestCase):
    # 大夹具,在所有测试方法执行之前打开浏览器
    @classmethod
    def setUpClass(cls) -> None:
        # 打开浏览器
        driver = open_browser()
        # 创建管理员登录page和登录后page的实例对象
        cls.admin = Admin(driver)
        cls.after = After(driver)

    @ddt.data(*admin_account)
    def test_success(self, data):
        # 请求网址
        self.admin.get(self.admin.admin_url)
        # 隐式等待
        self.admin.implicitly_wait()
        # 点击关闭广告
        self.admin.btn_close()
        # 点击请切换登录按钮
        self.admin.btn_login_switch()
        # 登录操作
        self.admin.ipt_username_admin(data['username'])
        self.admin.ipt_password_admin(data['password'])
        self.admin.btn_login_admin()
        # 切换frame
        self.admin.switch_frame('menu-frame')
        # 断言登录成功
        username = self.after.get_admin_name()
        self.assertEqual(data['username'], username)

    @ddt.data(*admin_fail_account)
    def test_fail(self, data):
        # 请求网址
        self.admin.get(self.admin.admin_url)
        self.admin.implicitly_wait()
        # 点击关闭广告
        self.admin.btn_close()
        # 点击请切换登录按钮
        self.admin.btn_login_switch()
        # 登录操作
        self.admin.ipt_username_admin(data['username'])
        self.admin.ipt_password_admin(data['password'])
        self.admin.btn_login_admin()
        # 断言登录失败
        username = self.after.get_admin_name()
        self.assertIsNone(username)

    # 小夹具,每个账号登录后刷新浏览器
    def tearDown(self) -> None:
        self.admin.browser_refresh()

    # 大夹具,关闭浏览器
    @classmethod
    def tearDownClass(cls) -> None:
        cls.after.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)


